package com.mes.web.controller.jy;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.mes.entity.jy.Productroom;
import com.mes.entity.jy.Queryjy;
import com.mes.service.jy.CarRoomServise;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: CarRoomController 
 * @Description:车间管理
 * @Author jy
 * @Date 2017年12月28日 下午3:53:44 
 * @version 1.0
 */
@Controller
@RequestMapping("/carroom")
public class CarRoomController {
	
	@Resource
	private CarRoomServise carRoomServise;

	/**
	 * 添加车间
	 * @param productroom 车间实体
	 * @param response 响应
	 * @throws IOException 输入输出异常
	 */
	@RequestMapping("/createCarRoom")
	public void createCarRoom(Productroom productroom ,HttpServletResponse response) throws IOException {
		//添加前先判断是否已经存在
		int affact = 0;
		int count = carRoomServise.selectCarRoomByName(productroom.getName());
		if(count > 0) {
			affact = 2;
		}else {
			affact = carRoomServise.createCarRoom(productroom);
		}
		response.getWriter().print(affact);
	}
	
	/**
	 * 分页查询车间
	 * @param queryjy 条件查询实体
	 * @param request 请求
	 * @param name 车间名称
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectAllProductRoom")
	public void selectAllProductRoom(Queryjy queryjy, HttpServletRequest request, String name,HttpServletResponse response) throws Exception {
		
		/*if(queryjy.getName()!= null) {
			
			String carroom = queryjy.getName();
			carroom = new String(carroom .getBytes("iso8859-1"),"utf-8");
			queryjy.setName(carroom);
		}*/
		
		int count = carRoomServise.countCarRoom(queryjy);
		List<Productroom> list= carRoomServise.selectCarRoomPagine(queryjy);
		 
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", count);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据id查询车间
	 * @param id 车间id
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/toEditCarRoom")
	public void toEditCarRoom(long id,HttpServletResponse response) throws Exception {
		Productroom productroom = carRoomServise.selectCarRoomByid(id);
		Gson gson = new Gson();
	    String json = gson.toJson(productroom);
	    ResponseUtil.write(response, json);
	}
	
	/**
	 * 修改车间
	 * @param productroom 车间实体
	 * @param response 响应
	 * @throws IOException 输入输出异常
	 */
	@RequestMapping("/editCarRoom")
	public void editCarRoom(Productroom productroom ,HttpServletResponse response) throws IOException {
		//修改前判断是否已存在
		int affact = 0;
		int count = carRoomServise.selectCarRoomByName(productroom.getName());
		if(count > 1) {
			affact = 2;
		}else {
			affact = carRoomServise.editCarRoom(productroom);
		}
		response.getWriter().print(affact);
	}
	
	/**
	 * 删除车间（批量）
	 * @param ids 所有id以“，”号拼接的字符串
	 * @param response 响应
	 * @throws IOException 异常
	 */
	@RequestMapping("/deleteCarRoom")
	public void deleteCarRoom(String ids,HttpServletResponse response) throws IOException {
		int affact = carRoomServise.deleteCarRoom(ids);
		response.getWriter().print(affact);
	}
	
	/**
	 * 导出
	 * @param response 响应
	 * @param request 请求
	 * @param queryjy 条件查询实体
	 * @throws UnsupportedEncodingException 抛出UnsupportedEncoding异常
	 */
	@RequestMapping("/export")
	public void export(HttpServletResponse response,HttpServletRequest request,Queryjy queryjy) throws UnsupportedEncodingException {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("车间信息表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("车间名称");
		cell.setCellStyle(style);
		
		//做判断，是不是导出所有
		List<Productroom> list = new ArrayList<Productroom>();
		if(queryjy.getType().equals("1")) {
			// 导出当前页
			list = carRoomServise.selectCarRoomPagine(queryjy);
		}else {
			// 导出所有
			list = carRoomServise.selectCarRoomAll();
		}
		
		
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow((int) i + 1);
			Productroom productroom = list.get(i);
			row.createCell(0).setCellValue(i + 1);
			row.createCell(1).setCellValue(productroom.getName());
		}
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String nowtime = sdf.format(now);
		String fileName = nowtime + "车间信息表.xls";
		fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
		try {
			response.reset();
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			OutputStream output = response.getOutputStream();
			BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
			bufferedOutPut.flush();
			wb.write(bufferedOutPut);
			bufferedOutPut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
